package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.bean.Pager;
import com.aiwei.tdjk.dao.extend.SaleOrderHeaderExtendMapper;
import com.aiwei.tdjk.dto.request.SaleOrderHeaderPagerRequest;
import com.aiwei.tdjk.entity.SaleOrderHeader;
import com.aiwei.tdjk.entity.SaleOrderHeaderExample;
import com.aiwei.tdjk.service.SaleOrderHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaleOrderHeaderServiceImpl implements SaleOrderHeaderService {

    @Autowired
    private SaleOrderHeaderExtendMapper saleOrderHeaderExtendMapper;


    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public Pager<SaleOrderHeader> find(SaleOrderHeaderPagerRequest request) {
        request.webParamConvert();
        List<SaleOrderHeader> list = saleOrderHeaderExtendMapper.find(request);
        int totalCount = saleOrderHeaderExtendMapper.getTotalCount(request);
        Pager<SaleOrderHeader> pager = new Pager<>();
        pager.setPageNumber(request.getPageNumber());
        pager.setPageSize(request.getPageSize());
        pager.setList(list);
        pager.setTotalCount(totalCount);
        return pager;
    }

    @Override
    public SaleOrderHeader get(Long id) {
        return saleOrderHeaderExtendMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据订单号查询订单信息
     *
     * @param saleOrderCode
     * @return
     */
    @Override
    public SaleOrderHeader getBySaleOrderCode(String saleOrderCode) {
        SaleOrderHeaderExample example = new SaleOrderHeaderExample();
        example.or().andSaleordercodeEqualTo(saleOrderCode);

        List<SaleOrderHeader> saleOrderHeaders = saleOrderHeaderExtendMapper.selectByExample(example);
        if (saleOrderHeaders!=null&& saleOrderHeaders.size()>0){
            return saleOrderHeaders.get(0);
        }
        return null;
    }

    /**
     * 新增
     *
     * @param saleOrderHeader
     * @return
     */
    @Override
    public int add(SaleOrderHeader saleOrderHeader) {
        return saleOrderHeaderExtendMapper.insertSelective(saleOrderHeader);
    }

    /**
     * 更新
     *
     * @param saleOrderHeader
     */
    @Override
    public void update(SaleOrderHeader saleOrderHeader) {
        saleOrderHeaderExtendMapper.updateByPrimaryKeySelective(saleOrderHeader);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        saleOrderHeaderExtendMapper.deleteByPrimaryKey(id);
    }

}
